﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LumenLiquid.Input
{
    class KeyboardLayout : IInputDevice
    {
        private KeyboardState _keyboardCurrent, _keyboardOld;
        private MouseState _mouseCurrent, _mouseOld;
        private Keys[] _mappedKeys;
        private bool _lockCursor;

        public bool KeyPressed(InputHandler.Keys key)
        {
            switch (key)
            {
                case InputHandler.Keys.MenuUp:
                    return KeyDown(Keys.W) || KeyDown(Keys.Up);
                case InputHandler.Keys.MenuDown:
                    return KeyDown(Keys.S) || KeyDown(Keys.Down);
                case InputHandler.Keys.MenuLeave:
                    return KeyDown(Keys.Escape) || KeyDown(Keys.Back);
                case InputHandler.Keys.MenuDecrease:
                    return KeyDown(Keys.A) || KeyDown(Keys.Left);
                case InputHandler.Keys.MenuIncrease:
                    return KeyDown(Keys.D) || KeyDown(Keys.Right);
                case InputHandler.Keys.MenuEnter:
                    return KeyDown(Keys.Space) || KeyDown(Keys.Enter);
                default:
                    return KeyDown(_mappedKeys[(int)key]);
            }
        }

        private bool KeyDown(Keys key)
        {
            return _keyboardCurrent.IsKeyDown(key) && _keyboardOld.IsKeyUp(key);
        }

        public Vector2 GetMovement()
        {
            Vector2 result = Vector2.Zero;
            if (KeyDown(_mappedKeys[(int)InputHandler.Keys.MoveForward])) result.Y = 1;
            else if (KeyDown(_mappedKeys[(int)InputHandler.Keys.MoveBackward])) result.Y = -1;
            if (KeyDown(_mappedKeys[(int)InputHandler.Keys.MoveRight])) result.X = 1;
            else if (KeyDown(_mappedKeys[(int)InputHandler.Keys.MoveLeft])) result.X = -1;
            return result;
        }

        public Vector2 GetCamera()
        {
            Vector2 result = Vector2.Zero;
            result.X = _mouseCurrent.X - Main.Instance.Screensize.X;
            result.Y = _mouseCurrent.Y - Main.Instance.Screensize.Y;
            return result;
        }

        public bool IsMapable(InputHandler.Keys key)
        {
            return key < InputHandler.Keys.MenuKeysFollowing;
        }

        public bool Remap(InputHandler.Keys key, int newKey)
        {
            return false; //TODO implement remapping
        }

        public bool HasCursor()
        {
            return true;
        }

        public Vector2 CursorPosition()
        {
            return new Vector2(_mouseCurrent.X, _mouseCurrent.Y);
        }

        public void LockCursor(bool lockCursor)
        {
            _lockCursor = lockCursor;
        }

        public void Update(GameTime gameTime)
        {
            _keyboardOld = _keyboardCurrent;
            _mouseOld = _mouseCurrent;

            _keyboardCurrent = Keyboard.GetState();
            _mouseCurrent = Mouse.GetState();

            if (_lockCursor)
                Mouse.SetPosition((int)Main.Instance.Screensize.X, (int)Main.Instance.Screensize.Y);
        }
    }
}
